/* Copyright 2012 Nathan Begle
    This file is part of pcoursemanager.

    pcoursemanager is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    pcoursemanager is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with pcoursemanager.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;

import javax.swing.GroupLayout;


public class Period_Window {
	Frame frame;
	Label title_lbl;
	TextField per_fld;
	Button ok_bt;
	
	Panel main_Panel;
	GroupLayout main_Layout;
	
	public Period_Window(ActionListener al, WindowListener wl, long curPer)
	{
		Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("Content/pcm_main.png"));
		frame = new Frame("Set Period");
		frame.setIconImage(image);
		frame.addWindowListener(wl);
		
		main_Panel = new Panel();
		main_Layout = new GroupLayout(main_Panel);
		main_Panel.setLayout(main_Layout);
		
		title_lbl = new Label("Enter a new update period in minutes (>10):");
		per_fld = new TextField(Long.toString(curPer/60/1000));
		
		ok_bt = new Button("Ok");
		ok_bt.setActionCommand("per_bt");
		ok_bt.addActionListener(al);
		
		buildLayout();
		
		frame.add(main_Panel);
		frame.pack();
		frame.setLocation(new Point(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - frame.getSize().width / 2,
				Toolkit.getDefaultToolkit().getScreenSize().height / 2 - frame.getSize().height / 2));
		frame.setVisible(true);
	}
	
	void buildLayout()
	{
		main_Layout.setVerticalGroup(main_Layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(main_Layout.createParallelGroup()
						.addComponent(title_lbl))
				.addGroup(main_Layout.createParallelGroup()
						.addComponent(per_fld))
				.addContainerGap()
				.addGroup(main_Layout.createParallelGroup()
						.addComponent(ok_bt))
				.addContainerGap());
		main_Layout.setHorizontalGroup(main_Layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(main_Layout.createParallelGroup()
						.addComponent(title_lbl).addComponent(per_fld).addComponent(ok_bt))
				.addContainerGap());
	}
	
	public void disposeWindow()
	{
		frame.dispose();
	}
	
	public void resetErr()
	{
		per_fld.setBackground(Color.WHITE);
	}
	
	public long getPeriod()
	{
		long temp = -1;
		try
		{
			temp = Long.parseLong(per_fld.getText());
			if(temp >= 10) { 
				return temp*60*1000; 
			} else { 
				per_fld.setBackground(new Color(1.0f, 0.7f, 0.7f));
				return -1;
			}
		} catch(Exception e) {
			per_fld.setBackground(new Color(1.0f, 0.7f, 0.7f));
			return -1;
		}
	}
	
	public void bringFront()
	{
		frame.toFront();
	}
	
	public boolean hasFocus()
	{
		return frame.isFocused();
	}
}
